//
//  ViewControllerTwo.m
//  HBDemo
//
//  Created by RYX on 2019/3/25.
//  Copyright © 2019 tuobu. All rights reserved.
//

#import "ViewControllerTwo.h"
#import "YHBNameCell.h"
#import "ViewControllerThree.h"
#import "YHBZDStore.h"
#import "YHBZDNameModel.h"

@interface ViewControllerTwo ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,copy) NSArray *listArr;
@end

@implementation ViewControllerTwo

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initCSS];
}
- (void)initCSS{
    
    [self setNavBarWithTitle:@"已划拨终端"];
    self.tableView = [[UITableView alloc] init];
    _tableView.backgroundColor = [UIColor clearColor];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorColor = [UIColor colorWithHexString:@"DEDEDE"];
//    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.showsVerticalScrollIndicator = NO;
    _tableView.showsHorizontalScrollIndicator = NO;
    _tableView.separatorInset = UIEdgeInsetsZero;
    [_tableView setTableFooterView:[[UIView alloc] initWithFrame:CGRectZero]];
    [self.view addSubview:_tableView];
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    MJRefreshNormalHeader *tabheader = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self loadData];
    }];
    tabheader.lastUpdatedTimeLabel.hidden = YES;
    _tableView.mj_header = tabheader;
    [self.tableView.mj_header beginRefreshing];
}
#pragma mark - UITableViewDelegate,UITableViewDataSource
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 44;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.listArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    YHBNameCell *cell = [YHBNameCell tempWithTableView:tableView];
    cell.model = self.listArr[indexPath.row];
//    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    YHBZDNameModel *model = self.listArr[indexPath.row];
    
    ViewControllerThree *VC = [[ViewControllerThree alloc] init];
    VC.targetId = model.toId;
    VC.realName = model.realName;
    [self.navigationController pushViewController:VC animated:YES];
}

- (void)loadData{
    
    YHBZDStore *store = [[YHBZDStore alloc] init];
    
    MJWeakSelf
    [store getYHBZDNameListWithCustomerId:[UserDefaultsManager sharedInstance].user_id Success:^(NSArray * _Nonnull listArr) {
    
        [weakSelf.tableView.mj_header endRefreshing];
        weakSelf.listArr = listArr;
        [weakSelf.tableView reloadData];
    
    } Failure:^(NSError * _Nonnull error) {
        
        [weakSelf.tableView.mj_header endRefreshing];
        NSLog(@"请求出错");
    }];
}
@end
